﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

using crmbl.Azure.Diagnostics.EventLogs.Processor;
using crmbl.Azure.Diagnostics.Logs.Processor;
using crmbl.Azure.Diagnostics.InfrastructureLogs.Processor;
using crmbl.Azure.Diagnostics.PerformanceCounters.Processor;
using crmbl.Azure.Metadata.Globals;

using crmbl.Azure.Globals;

using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace crmbl.Azure.Test.Processor
{
    class Program
    {
        static void Main(string[] args)
        {
            String APPCONFIG_CONFIGURATIONSTRING = ConfigurationManager.ConnectionStrings["ApplicationDiagnosticsDatabase"].ConnectionString;

            //TODO: Encrypt and decrypt passwords
            /*
            X509Certificate2 cert = EncryptionHelper.LoadCertificate(System.Security.Cryptography.X509Certificates.StoreName.My,System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, "AzureWatcher");

            byte[] encoded = System.Text.UTF8Encoding.UTF8.GetBytes("Encrypt me");

            byte[] encrypted = EncryptionHelper.Encrypt(encoded, true, cert);
            byte[] decrypted = EncryptionHelper.Decrypt(encrypted, true, cert);

            System.Console.Out.WriteLine(System.Text.UTF8Encoding.UTF8.GetString(decrypted));
            */

            while (1 == 1)
            {

                using (ApplicationHelper app = new ApplicationHelper(APPCONFIG_CONFIGURATIONSTRING))
                {

                    foreach (StorageAccount storageAccount in app.GetStorageAccounts())
                    {

                        /* WAD Log Processor*/
                        using (LogsProcessor logProcessor = new LogsProcessor (storageAccount.StorageAccountId, storageAccount.StorageAccountName, storageAccount.StorageAccountKey, APPCONFIG_CONFIGURATIONSTRING))
                        {
                            logProcessor.Process();
                        }
                        /**/

                        /* Event Logs*/
                        using (EventLogProcessor evtProcessor = new EventLogProcessor(storageAccount.StorageAccountId, storageAccount.StorageAccountName, storageAccount.StorageAccountKey,APPCONFIG_CONFIGURATIONSTRING))
                        {
                            evtProcessor.Process();
                        }
                        /* */

                        /* Performance Counters*/
                        using (PerformanceCounterProcessor perfProcessor = new PerformanceCounterProcessor(storageAccount.StorageAccountId, storageAccount.StorageAccountName, storageAccount.StorageAccountKey, APPCONFIG_CONFIGURATIONSTRING))
                        {
                            perfProcessor.Process();
                        }
                         /* */

                        /* WAD Infra */
                        /*
                       using (InfrastructureLogProcessor infraProcessor = new InfrastructureLogProcessor(storageAccount.StorageAccountId, storageAccount.StorageAccountName, storageAccount.StorageAccountKey,APPCONFIG_CONFIGURATIONSTRING))
                       {
                           infraProcessor.Process();
                       }
                       */
                        /**/

                    }
                }

            }
            //Console.WriteLine("DONE");
            //Console.ReadLine();
        }
    }
}
